"""
@author: edgardeng
@date:   2021-06-02.
-  使用Skleran 计算 roc

sklearn.metrics中的roc_curve方法（metrics是度量、指标，curve是曲线）

roc_curve(y_true, y_score, pos_label=None, sample_weight=None, drop_intermediate=None)

参数含义：
 * y_true：简单来说就是label，范围在(0，1)或(-1，1)的二进制标签，若非二进制则需提供pos_label。
 * y_score：模型预测的类别概率值。
 * pos_label：label中被认定为正样本的标签。若label=[1,2,2,1]且pos_label=2，则2为positive，其他为negative。
 * sample_weight：采样权重，可选择一部分来计算。
 * drop_intermediate：可以去掉一些对ROC曲线不好的阈值，使得曲线展现的性能更好。
 * 返回值：（tpr，fpr，thershold）
 *  tpr：根据不同阈值得到一组tpr值。
 * fpr：根据不同阈值的到一组fpr值，与tpr一一对应。（这两个值就是绘制ROC曲线的关键）
 * thresholds：选择的不同阈值，按照降序排列。
"""
import numpy as np
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt

def draw_roc():
    # y_label = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1]
    # y_pre = [0.5246785283088684, 0.951103925704956, 0.9764867424964905, 0.9726530313491821, 0.9511547684669495, 0.9610205888748169, 0.9431692957878113, 0.9297606945037842, 0.9565743207931519, 0.8762696981430054, 0.8367423415184021, 0.9577385783195496, 0.9463088512420654, 0.9754701852798462, 0.9635430574417114, 0.9605380892753601, 0.9463150501251221, 0.9666976928710938, 0.955309271812439, 0.9581801891326904, 0.8289101719856262, 0.6922315359115601, 0.6897686123847961, 0.8696495294570923, 0.732205867767334, 0.4475182294845581, 0.9037160277366638, 0.9576685428619385, 0.9247936606407166, 0.9644496440887451, 0.9524257779121399, 0.43085983395576477, 0.7409199476242065, 0.31444695591926575, 0.9507156610488892, 0.3363935053348541, 0.9654169082641602, 0.9622114300727844, 0.9604854583740234, 0.9189552068710327, 0.324629545211792, 0.9116954207420349, 0, 0.8144199848175049, 0.9534525871276855, 0.7200265526771545, 0.9236580729484558, 0.9415320158004761, 0.9616016149520874, 0.9607740044593811, 0.9579656720161438, 0.9599236845970154, 0.944079577922821, 0.652263343334198, 0.3021731972694397, 0.8698753118515015, 0.3052281439304352, 0.9283860921859741, 0.9479116797447205, 0.9747918248176575, 0.9363546371459961, 0.8843031525611877, 0.9368022084236145, 0.3231445252895355, 0.9344288110733032, 0.9417835474014282, 0.8845003843307495, 0.6576707363128662, 0.8666821122169495, 0.8261072039604187, 0.9362092018127441, 0.9477815628051758, 0.7619313597679138, 0, 0.7755676507949829, 0.9095929861068726, 0.585529625415802, 0.9084796905517578, 0.9559529423713684, 0.851840078830719, 0.925463855266571, 0.9444829821586609, 0.9462170600891113, 0.909339427947998, 0.8476184606552124, 0.7776787281036377, 0.9465910196304321, 0.8429244756698608, 0.6981208324432373, 0.6873844861984253, 0.8668627142906189, 0.8576093912124634, 0.9634454250335693, 0.9251115322113037, 0.8102371692657471, 0.9582592248916626, 0, 0.9440770745277405, 0.9409496188163757, 0.6525720953941345, 0.9713034629821777, 0.9694364666938782, 0.9645647406578064, 0.9644237160682678, 0.9150046110153198, 0.9809229373931885, 0.7427013516426086, 0.46999824047088623, 0.961273729801178]

    y_label = np.random.randint(2, size=100)
    y_pre = np.random.rand(100)
    fpr, tpr, thersholds = roc_curve(y_label, y_pre)

    if thersholds[0] > 1:
        thersholds[0] = 1
    fpr = np.around(fpr, decimals=4).tolist()
    tpr = np.around(tpr, decimals=4).tolist()
    thersholds = np.around(thersholds, decimals=3).tolist()

    print('fpr',fpr)
    print('tpr',tpr)
    print('thersholds',thersholds)
    roc_auc = auc(fpr, tpr)
    print('auc', roc_auc)

    plt.plot(fpr, tpr, 'k--', label='ROC (area = {0:.2f})'.format(roc_auc), lw=2)
    plt.plot(fpr, thersholds, 'r--', label='thersholds', lw=2)
    plt.xlim([-0.05, 1.05])  # 设置x、y轴的上下限，以免和边缘重合，更好的观察图像的整体
    plt.ylim([-0.05, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')  # 可以使用中文，但需要导入一些库即字体
    plt.title('ROC Curve')
    plt.legend(loc="lower right")
    plt.show()


if __name__ == '__main__':
    draw_roc()